﻿using ShareFrame.Entity.Base_Manage;
using ShareFrame.Entity.DTO;
using ShareFrame.Util;
using ShareFrame.Util.Helper;
using Dapper;
using EFCore.Sharding;
using LinqKit;
using Microsoft.EntityFrameworkCore;
using System.Collections.Generic;
using System.Linq;
using System.Linq.Dynamic.Core;
using System.Threading.Tasks;
using ShareFrame.IBusiness.Base_Manage;

namespace ShareFrame.Business.Base_Manage
{
    /// <summary>
    /// 站内消息记录接口实现类
    /// </summary>
    public class Base_MessageRecordsBusiness : BaseBusiness<Base_MessageRecords>, IBase_MessageRecordsBusiness, ITransientDependency
    {
        public Base_MessageRecordsBusiness(IDbAccessor db)
            : base(db)
        {
        }

        #region 外部接口
        /// <summary>
        /// 根据条件获取分页结果集
        /// </summary>
        /// <param name="input">传入条件参数</param>
        /// <returns></returns>
        public async Task<PageResult<Base_MessageRecords>> GetDataListAsync(PageInput<ConditionDTO> input)
        {
            var q = GetIQueryable();
            var where = LinqHelper.True<Base_MessageRecords>();
            var search = input.Search;

            //筛选
            if (!search.Condition.IsNullOrEmpty() && !search.Keyword.IsNullOrEmpty())
            {
                var newWhere = DynamicExpressionParser.ParseLambda<Base_MessageRecords, bool>(
                    ParsingConfig.Default, false, $@"{search.Condition}.Contains(@0)", search.Keyword);
                where = where.And(newWhere);
            }

            return await q.Where(where).GetPageResultAsync(input);
        }

        /// <summary>
        /// 根据多条件动态获取分页结果集
        /// </summary>
        /// <param name="basedbOptions">数据库连接</param>
        /// <param name="input">传入条件参数集</param>
        /// <returns></returns>
        public async Task<PageResult<Base_MessageRecords>> GetDataListAsync(BaseDbOption basedbOptions, PageInput<List<ConditionDTO>> input)
        {
            var dapper = new DapperHelper<Base_MessageRecords>(basedbOptions.ConnectionString);

            string sql = "SELECT * FROM Base_MessageRecords WHERE 1 = 1";
            string sqlcount = "SELECT COUNT(*) FROM Base_MessageRecords WHERE 1 = 1";
            DynamicParameters param = new DynamicParameters();

            for (int i = 0; i < input.Search.Count; i++)
            {
                sql = string.Format("{0} AND {1} = @value{2}", sql, input.Search[i].Condition, i);
                sqlcount = string.Format("{0} AND {1} = @value{2}", sqlcount, input.Search[i].Condition, i);
                param.Add(string.Format("value{0}", i), input.Search[i].Keyword);
            }
            sql = string.Format("{0} LIMIT {1},{2}", sql, (input.PageIndex - 1) * input.PageRows, input.PageRows);
            return await dapper.ExcuteSqlByPaging(sql, param, sqlcount);
        }

        /// <summary>
        /// 根据传入Id获取对应实体信息
        /// </summary>
        /// <param name="id">实体唯一编号</param>
        /// <returns></returns>
        public async Task<Base_MessageRecords> GetTheDataAsync(string id)
        {
            return await GetEntityAsync(id);
        }

        /// <summary>
        /// 基础添加方法
        /// </summary>
        /// <param name="data">要添加的实体信息</param>
        /// <returns></returns>
        public async Task AddDataAsync(Base_MessageRecords data)
        {
            await InsertAsync(data);
        }

        /// <summary>
        /// 批量添加数据-低性能
        /// </summary>
        /// <param name="list">要添加的实体集合</param>
        /// <returns></returns>
        public async Task AddDataExlAsync(List<Base_MessageRecords> list)
        {
            await InsertAsync(list);
        }

        /// <summary>
        /// 批量添加数据,速度快
        /// </summary>
        /// <param name="list">要添加的实体集合</param>
        public void BulkInsertInterface(List<Base_MessageRecords> list)
        {
            BulkInsert(list);
        }

        /// <summary>
        /// 基础更新方法
        /// </summary>
        /// <param name="data">要更新的实体信息</param>
        /// <returns></returns>
        public async Task UpdateDataAsync(Base_MessageRecords data)
        {
            await UpdateAsync(data);
        }

        /// <summary>
        /// 重写更新方法-批量更新方法
        /// </summary>
        /// <param name="basedbOptions">数据库连接字符串对象</param>
        /// <param name="columnName">更新列名</param>
        /// <param name="upValue">更新值</param>
        /// <param name="ids">ID集合</param>
        /// <returns></returns>
        public async Task UpdateDataAsync(BaseDbOption basedbOptions, string columnName, string upValue, List<string> ids)
        {
            var dapper = new DapperHelper<int>(basedbOptions.ConnectionString);

            string sql = string.Format("UPDATE Base_MessageRecords SET {0} = @upValue WHERE Id = @Id",columnName);

            for (int i = 0; i < ids.Count; i++)
            {
                await dapper.ExecuteSqlAsync(sql, new { upValue = upValue, Id = ids[i] });
            }
        }
        public async Task UpdateDataAsync(BaseDbOption basedbOptions, string columnName, int upValue, List<string> ids)
        {
            var dapper = new DapperHelper<int>(basedbOptions.ConnectionString);

            string sql = string.Format("UPDATE Base_MessageRecords SET {0} = @upValue WHERE Id = @Id", columnName);

            for (int i = 0; i < ids.Count; i++)
            {
                await dapper.ExecuteSqlAsync(sql, new { upValue = upValue, Id = ids[i] });
            }
        }
        public async Task UpdateDataAsync(BaseDbOption basedbOptions, string columnName, bool upValue, List<string> ids)
        {
            var dapper = new DapperHelper<int>(basedbOptions.ConnectionString);

            string sql = string.Format("UPDATE Base_MessageRecords SET {0} = @upValue WHERE Id = @Id", columnName);

            for (int i = 0; i < ids.Count; i++)
            {
                await dapper.ExecuteSqlAsync(sql, new { upValue = upValue, Id = ids[i] });
            }
        }

        /// <summary>
        /// 基础删除方法
        /// </summary>
        /// <param name="ids">传入条件参数</param>
        /// <returns></returns>
        public async Task DeleteDataAsync(List<string> ids)
        {
            await DeleteAsync(ids);
        }

        #endregion

        #region 私有成员

        #endregion
    }
}